<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    :paddBottom="5"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <div class="title">
      <el-tag>操作步骤说明：</el-tag>
      <el-tag>第一步：查询本期所有下属单位报送记录</el-tag>
      <el-tag>第二步：审核下属单位数据</el-tag>
      <el-tag>第三步：对未上报单位进行催报</el-tag>
    </div>
    <lb-table
      :column="tableData.column"
      :data="tableData.data"
      border
      :height="550"
      pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[20, 40, 60, 80]"
      :pager-count="5"
      :current-page.sync="currentPage"
      :total="total"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @p-current-change="handleCurrentChange"
      @selection-change="handleSelectionChange"
    >
    </lb-table>
    <component
      :is="modelName"
      :id="id"
      :visible.sync="show"
      @close="close"
    ></component>
  </lb-layout>
</template>

<script>
import {
  getLowLevellist,
  getLedgerpass,
  listDept,
} from "@/api/standingbook/newstandingbook.js";
// import { listDept } from "@/api/system/dept.js";

import operate from "@/views/work/accounts/audit/components/operate.vue";
export default {
  name: "",
  components: {
    operate,
  },
  dicts: ["ledger_report_status", "ledger_audit_status"],
  data() {
    return {
      //弹窗
      id: "",
      modelName: "",
      show: false,
      showDeptArr: [], // 单位集合
      apsForm: {
        formRef: "form",
        labelWidth: "auto",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            deptName: {
              label: "单位名称:",
              render: (h) => {
                let that = this;
                let arr = [...that.showDeptArr];
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().deptName;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择单位名称",
                        value: valNum,
                        clearable: true,
                        filterable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("deptName", value);
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: label,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            superiorDeptName: {
              label: "上级单位:",
              render: (h) => {
                let that = this;
                let arr = [...that.showDeptArr];
                // 数据严谨处理,处理后端数据异常情况
                let valNum =
                  that.$refs?.form?.getFieldsValue().superiorDeptName;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择上级单位",
                        value: valNum,
                        clearable: true,
                        filterable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue(
                            "superiorDeptName",
                            value
                          );
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: label,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            year: {
              label: "年份:",
              typeName: "el-date-picker",
              type: "year",
              size: "small",
              placeholder: "选择年份",
              "value-format": "yyyy",
            },
            month: {
              label: "时间段:",
              typeName: "el-select",
              render: (h) => {
                let that = this;
                let arr = [];
                arr = [
                  {
                    label: "一月",
                    value: "01",
                  },
                  {
                    label: "二月",
                    value: "02",
                  },
                  {
                    label: "三月",
                    value: "03",
                  },
                  {
                    label: "四月",
                    value: "04",
                  },
                  {
                    label: "五月",
                    value: "05",
                  },
                  {
                    label: "六月",
                    value: "06",
                  },
                  {
                    label: "七月",
                    value: "07",
                  },
                  {
                    label: "八月",
                    value: "08",
                  },
                  {
                    label: "九月",
                    value: "09",
                  },
                  {
                    label: "十月",
                    value: "10",
                  },
                  {
                    label: "十一月",
                    value: "11",
                  },
                  {
                    label: "十二月",
                    value: "12",
                  },
                ];
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().month;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择时间段",
                        value: valNum,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("month", value);
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: value,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            ledgerAuditStatus: {
              label: "审核状态:",
              typeName: "el-select",
              render: (h) => {
                let that = this;
                let arr = [];
                that.dict?.type?.ledger_audit_status.some((item) => {
                  arr.push(item);
                });
                let valNum =
                  that.$refs?.form?.getFieldsValue().ledgerAuditStatus;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择审核状态",
                        value: valNum,
                        clearable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue(
                            "ledgerAuditStatus",
                            value
                          );
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: value,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            ledgerReportStatus: {
              label: "上报状态:",
              typeName: "el-select",
              render: (h) => {
                let that = this;
                let arr = [];
                that.dict?.type?.ledger_report_status.some((item) => {
                  arr.push(item);
                });
                let valNum =
                  that.$refs?.form?.getFieldsValue().ledgerReportStatus;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择审核状态",
                        value: valNum,
                        clearable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue(
                            "ledgerReportStatus",
                            value
                          );
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: value,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
            {
              label: "批量审核",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-document-checked",
              click: this.handlerbatchReport,
            },
            {
              label: "催报所有未上报",
              type: "primary",
              typeName: "el-button",
              plain: true,
              size: "small",
              icon: "el-icon-message-solid",
              click: () => {
                setTimeout(() => {
                  this.$message.success("催报所有未上报成功");
                }, 200);
              },
            },
          ],
        },
      },
      currentPage: 1,
      pageSize: 20,
      total: 0,
      tableData: {
        column: [
          {
            type: "selection",
            selectable: this.checkSelect,
          },
          {
            prop: "year",
            label: "年度",
            width: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "organizationName",
            label: "报送单位",
            minWidth: 180,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let that = this;
              let name = scope.row.organizationName;
              let type = scope.row.summeryTable === 0 ? "2" : "1"; // 0 不是汇总数据 1是汇总数据
              let a =
                scope.row.summeryTable === 0 ? (
                  <router-link
                    to={
                      "/work/standbook/newadds/add2?id=" +
                      scope.row.id +
                      "&year=" +
                      scope.row.year +
                      "&month=" +
                      scope.row.month +
                      "&disabled=1" +
                      "&deptId=" +
                      scope.row.deptId +
                      "&deptName=" +
                      scope.row.deptName +
                      "&createTime=" +
                      scope.row.createTime
                    }
                    class="link-type"
                  >
                    {name}
                  </router-link>
                ) : (
                  <router-link
                    to={
                      "/work/standbook/audits/collect?deptId=" +
                      scope.row.deptId +
                      "&year=" +
                      scope.row.year +
                      "&month=" +
                      scope.row.month +
                      "&type=" +
                      type +
                      "&id=" +
                      scope.row.id
                    }
                    class="link-type"
                  >
                    {name}
                  </router-link>
                );
              return <div>{a}</div>;
            },
          },
          {
            prop: "",
            label: "填报日期",
            width: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let that = this;
              let a = scope.row.year ?? "";
              let b = scope.row.month ?? "";
              let c = a + b;
              return h("div", c);
            },
          },
          {
            prop: "ledgerReportStatus",
            label: "上报状态",
            minWidth: 120,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.ledger_report_status}
                    value={scope.row.ledgerReportStatus}
                  />
                </div>
              );
            },
          },
          {
            prop: "ledgerAuditStatus",
            label: "审核状态",
            minWidth: 120,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.ledger_audit_status}
                    value={scope.row.ledgerAuditStatus}
                  />
                </div>
              );
            },
          },
          {
            prop: "statisticEmployee",
            label: "统计员",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "statisticsEmployeeMobilePhone",
            label: "手机号码",
            width: 160,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "updateTime",
            label: "更新时间",
            width: 180,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "ledgerEmployee",
            label: "审核人",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "specialSituationDescription",
            label: "备注",
            align: "center",
            showOverflowTooltip: true,
          },
          {
            label: "操作",
            width: 280,
            fixed: "right",
            render: (h, scope) => {
              let that = this;
              let a, b, c, d;
              let ledgerReportStatus = scope.row.ledgerReportStatus; // 上报状态
              let ledgerAuditStatus = scope.row.ledgerAuditStatus; // 审核状态
              let ledgerStatus = scope.row.ledgerStatus; // 台账省报状态
              switch (ledgerStatus) {
                case 0:
                case 3:
                  c = (
                    <el-button
                      type="text"
                      size="small"
                      icon="el-icon-message-solid"
                      onClick={() => {
                        setTimeout(() => {
                          that.$message.success("催报成功");
                        }, 200);
                      }}
                    >
                      系统催报
                    </el-button>
                  );
                  break;
                case 1:
                  a = (
                    <el-button
                      type="text"
                      size="small"
                      icon="el-icon-check"
                      onClick={() => {
                        that.handlerPass(scope.row);
                      }}
                    >
                      通过
                    </el-button>
                  );
                  b = (
                    <el-button
                      type="text"
                      size="small"
                      icon="el-icon-close"
                      onClick={() => {
                        that.showModel("operate", scope.row);
                      }}
                      class="text-danger"
                    >
                      退回
                    </el-button>
                  );
                  break;
              }
              // d = (
              //   <el-button
              //     type="text"
              //     size="small"
              //     icon="el-icon-view"
              //     onClick={() => {
              //       that.$router.push({
              //         name: "standingbookCollect",
              //         query: {
              //           deptId: scope.row.deptId,
              //           year: scope.row.year,
              //           month: scope.row.month,
              //         },
              //       });
              //     }}
              //   >
              //     查看
              //   </el-button>
              // );
              return (
                <div style="display: flex;align-items: center;justify-content: flex-start;">
                  {a}
                  {b}
                  {/* {d} */}
                  {c}
                </div>
              );
            },
          },
        ],
        data: [],
      },
      multipleSelection: [],
    };
  },
  created() {
    this.getlistDept();
  },
  mounted() {
    this.handlerSearchDetail();
  },
  methods: {
    getlistDept() {
      this.showDeptArr = [];
      listDept({})
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let arr = res?.data ?? [];
          if (arr.length > 0) {
            arr.map((item) => {
              let obj = {
                label: item?.deptName,
                value: item?.deptId,
              };
              this.showDeptArr.push(obj);
            });
          }
        })
        .catch(() => {
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // console.log(this.multipleSelection, "multipleSelection");
    },
    checkSelect(row) {
      if (row.ledgerStatus == 1) {
        return true;
      } else {
        return false;
      }
    },
    // 批量上报
    handlerbatchReport() {
      if (this.multipleSelection.length > 0) {
        let params = {
          ledgerIds: [],
        };
        this.multipleSelection.map((item) => {
          params.ledgerIds.push(item.id);
        });
        const text = "是否确认通过这些数据" + "？";
        this.$modal
          .confirm(text)
          .then(() => {
            getLedgerpass(params)
              .then((res) => {
                if (res.code === 700) {
                  this.$message.warning(res.msg);
                  return false;
                }
                this.$message.success("批量审核成功!");
                this.handlerSearchDetail();
              })
              .catch(() => {
                // this.$message.error("系统错误,请联系管理员!");
              });
          })
          .catch(() => {
            this.$message({
              message: "取消",
            });
          });
      } else {
        this.$message({
          message: "请先勾选需要批量审核的数据",
          type: "warning",
        });
      }
    },
    async initData(data) {
      let that = this;
      let query = {
        ...data,
      };
      query.pageNum = that.currentPage;
      query.pageSize = that.pageSize;
      that.$store.commit("app/updateApiCount", "add");
      await getLowLevellist(query)
        .then((res) => {
          that.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res?.rows ?? [];
          that.total = res?.total ?? 0;
        })
        .catch((err) => {
          that.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 搜索
    handlerSearchDetail() {
      const { year, month, ...other } = this.$refs?.form?.getFieldsValue();
      const data = {
        ...other,
      };
      if (year) {
        data.year = year + "年";
      }
      if (month) {
        data.month = month + "月";
      }
      data.deptId = this.$store.state.user.deptId;
      this.initData(data);
    },
    // 重置
    handleReset() {
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    // 通过
    handlerPass(row) {
      let id = row.id;
      const text = "是否确认通过" + "该条数据" + "？";
      this.$modal
        .confirm(text)
        .then(() => {
          this.requeAudit(id);
        })
        .catch(() => {
          this.$message({
            message: "取消",
          });
        });
    },
    // 审核请求
    requeAudit(id) {
      let params = {
        ledgerIds: [],
      };
      params.ledgerIds.push(id);
      getLedgerpass(params)
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            this.handlerSearchDetail();
            return false;
          }
          this.$message.success("审核成功!");
          this.handlerSearchDetail();
        })
        .catch(() => {
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 弹窗
    showModel(name, params) {
      this.modelName = name;
      this.id = params?.id || "";
      setTimeout(() => {
        this.show = true;
      }, 200);
    },
    close(status) {
      this.show = false;
      setTimeout(() => {
        if (status) this.handlerSearchDetail();
      }, 200);
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .title {
  .el-tag {
    margin-right: 10px !important;
    margin-bottom: 20px;
  }
}
</style>
